package cn.itcast.czxy.BD18

import org.apache.spark.ml.{Pipeline, PipelineModel}
import org.apache.spark.ml.classification.{DecisionTreeClassificationModel, DecisionTreeClassifier}
import org.apache.spark.ml.clustering.{KMeans, KMeansModel}
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature._
import org.apache.spark.sql.types.DoubleType
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

/*
* 用于实现使用k_means给鸢尾花数据分类
* */
object LrisJCS {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder().master("local[*]").appName("LrisJCS").getOrCreate()
    import spark.implicits._
    import org.apache.spark.sql.functions._
    val lrislibsvmDF: DataFrame = spark.read.csv("file:///E:\\教学\\学习资料4\\机器学习\\03挖掘型标签\\03挖掘型标签\\数据集\\iris_tree.csv")
      .toDF("Slength", "Swidth", "Plength", "Pwidth", "species")
      .select('Slength.cast(DoubleType), 'Swidth.cast(DoubleType), 'Plength.cast(DoubleType), 'Pwidth.cast(DoubleType), 'species)
    //    lrislibsvmDF.show(false)
    /*
    * +-------+------+-------+------+-----------+
      |Slength|Swidth|Plength|Pwidth|species    |
      +-------+------+-------+------+-----------+
      |5.1    |3.5   |1.4    |0.2   |Iris-setosa|
      |4.9    |3.0   |1.4    |0.2   |Iris-setosa|
    * */

    val speciesTolobel: StringIndexerModel = new StringIndexer()
      .setInputCol("species")
      .setOutputCol("label")
      .fit(lrislibsvmDF)

    //    speciesTolobel.transform(lrislibsvmDF).show()

    /*
    * +-------+------+-------+------+-----------+-----+
|Slength|Swidth|Plength|Pwidth|    species|label|
+-------+------+-------+------+-----------+-----+
|    5.1|   3.5|    1.4|   0.2|Iris-setosa|  0.0|
|    4.9|   3.0|    1.4|   0.2|Iris-setosa|  0.0|
|    4.7|   3.2|    1.3|   0.2|Iris-setosa|  0.0|
|    4.6|   3.1|    1.5|   0.2|Iris-setosa|  0.0|
|    5.0|   3.6|    1.4|   0.2|Iris-setosa|  0.0|
    * */

    val features: VectorAssembler = new VectorAssembler()
      .setInputCols(Array("Slength", "Swidth", "Plength", "Pwidth"))
      .setOutputCol("features")

    val decisionTree = new DecisionTreeClassifier().setFeaturesCol("features")
      .setPredictionCol("Prediction")
      .setMaxDepth(4)
    val pipeline: Pipeline = new Pipeline().setStages(Array(speciesTolobel, features, decisionTree))



    val Array(trainDatas, testDatas): Array[Dataset[Row]] = lrislibsvmDF.randomSplit(Array(0.8, 0.2))



    val model: PipelineModel = pipeline.fit(trainDatas)

    val testDF: DataFrame = model.transform(testDatas)

        testDF.show(false)
//    val evaluator: MulticlassClassificationEvaluator = new MulticlassClassificationEvaluator()
//      .setLabelCol("label")
//      .setPredictionCol("Prediction")
//    val score: Double = evaluator.evaluate(testDF)
//    println(">>>>>>>>" + score)
    //    val jcstux: DecisionTreeClassificationModel = model.stages(2).asInstanceOf[DecisionTreeClassificationModel]
    //    println(jcstux.toDebugString)
  }
}
